#pragma once

#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>

#define tsakNum 3

typedef void (*task_t)();


void Print()
{
    std::cout<<"Print"<<std::endl;
}

void DownLoad()
{
    std::cout<<"DownLoad"<<std::endl;
}

void Flush()
{
    std::cout<<"Flush"<<std::endl;
}

task_t tasks[tsakNum];

void Loadtask()
{
    srand(time(nullptr) ^ getpid());
    tasks[0] = Print;
    tasks[1] = DownLoad;
    tasks[2] = Flush;
}

void ExcuteTask(int number)
{
    if(number < 0 || number > 2) return;
    tasks[number]();
}

int SelectTask()
{
    return rand() % tsakNum;
}

void work()
{
    while (true)
    {
        int command = 0;
        int n = read(0, &command, sizeof(command));
        if (n == sizeof(int))
        {
            std::cout << "pid is " << getpid() << " hander task" << std::endl;
            ExcuteTask(command);
        }
        else if (n == 0)
        {
            std::cout << " sub process " << getpid() << " quit" << std::endl;
            break;
        }
    }
}